package PIPL::System::PathTest;

use strict;

use base 'Test::Unit::TestCase';

use PIPL::System::Path;
use PIPL::System::Config;

use Test::MockObject;

sub new {
    my $self = shift()->SUPER::new(@_);
    return $self;
}

sub test_PIPL_System_Path_new {
    my $self = shift;
    my $obj1 = PIPL::System::Path->new;
    $self->assert_not_null($obj1);
    $self->assert($obj1->isa('PIPL::System::Path'));
    my $obj2 = $obj1->new;
    $self->assert_not_null($obj2);
    $self->assert($obj2->isa('PIPL::System::Path'));
}

sub test_PIPL_System_Path_instance {
    my $self = shift;
    PIPL::System::Path->destroy;
    my $obj1 = PIPL::System::Path->instance;
    my $obj2 = PIPL::System::Path->instance;
    $self->assert_not_null($obj1);
    $self->assert_not_null($obj2);
    $self->assert($obj1->isa('PIPL::System::Path'));
    $self->assert($obj2->isa('PIPL::System::Path'));
    $self->assert_equals($obj1, $obj2);
}

sub test_PIPL_System_Path_tmpdir {
    my $self = shift;
    my $mock = Test::MockObject->new;
    $mock->fake_module(
        'File::Spec',
        tmpdir => sub { return '/tmp' }
    );
    my $tmpdir = File::Spec->tmpdir;
    $self->assert_equals($tmpdir, '/tmp');
    $self->assert_equals($tmpdir, PIPL::System::Path->tmpdir);
    $self->assert_equals($tmpdir, $PIPL::System::Path::Self->{'tmpdir'});
    my $obj = PIPL::System::Path->instance;
    $self->assert_not_null($obj);
    $self->assert($obj->isa('PIPL::System::Path'));
    $self->assert_equals($tmpdir, $obj->tmpdir);
    $self->assert_equals($tmpdir, $obj->{'tmpdir'});
}

sub test_PIPL_System_Path_home {
    my $self = shift;

    # set up
    my $home = sprintf('/home/test-%s-%s', time, $$);
    my $orig_Env_Self = $PIPL::System::Env::Self;
    $PIPL::System::Env::Self = {'HOME'=>$home};

    # main
    $self->assert_not_null(PIPL::System::Path->home);
    $self->assert_equals($home, PIPL::System::Path->home);
    
    # tear down
    $PIPL::System::Env::Self = $orig_Env_Self;
    PIPL::System::Path->destroy;
    my $obj1 = PIPL::System::Path->instance;
    $self->assert_not_null($obj1);
}

sub test_PIPL_System_Path_appdata {
    my $self = shift;

    # set up
    my $appdata = sprintf('/appdata/test-%s-%s', time, $$);
    my $home = sprintf('/home/test-%s-%s', time, $$);

    my $orig_Config_Self = $PIPL::System::Config::Self;
    $PIPL::System::Config::Self = {'osname'=>'MSWin32'};
    my $orig_Env_Self = $PIPL::System::Env::Self;
    $PIPL::System::Env::Self = {'APPDATA'=>$appdata, 'HOME'=>$home};

    # main
    $self->assert_not_null(PIPL::System::Path->appdata);
    $self->assert_equals($appdata, PIPL::System::Path->appdata);

    delete $PIPL::System::Env::Self->{APPDATA};
    PIPL::System::Path->destroy;
    $self->assert_equals($home, PIPL::System::Path->appdata);
    
    # tear down
    $PIPL::System::Config::Self = $orig_Config_Self;
    PIPL::System::Path->destroy;
    my $obj1 = PIPL::System::Path->instance;
    $self->assert_not_null($obj1);
    $PIPL::System::Env::Self = $orig_Env_Self;
    PIPL::System::Path->destroy;
    my $obj2 = PIPL::System::Path->instance;
    $self->assert_not_null($obj2);
}

1;
